package com.lu.study.lu.mybatis.plus.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.config.builder.ConfigBuilder;
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import com.baomidou.mybatisplus.generator.engine.AbstractTemplateEngine;
import com.baomidou.mybatisplus.generator.engine.VelocityTemplateEngine;

import javax.validation.constraints.NotNull;
import java.io.File;
import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
 * @CreateDate 2022/4/25 10:23 (星期一)
 * @ClassName LuCustom
 * @Name lu-framework
 * @Author RaoLuPing
 * @Version 1.0
 * @Description TODO
 **/
public class LuCustomVelocityTemplateEngine extends VelocityTemplateEngine {

    @Override
    protected void outputCustomFile(@NotNull Map<String, String> customFile, @NotNull TableInfo tableInfo, @NotNull Map<String, Object> objectMap) {
        String entityName = tableInfo.getEntityName();
        String otherPath = getPathInfo(OutputFile.other);
        customFile.forEach((key, value) -> {
            if ("Dto.java".equalsIgnoreCase(key)) {
                //extMap.put("modelName", modelName);
                String fileName = String.format((otherPath + "domain" + File.separator + "dto" + File.separator + StrUtil.upperFirst(tableInfo.getName()) + "%s"), key);
                outputFile(new File(fileName), objectMap, value);
            } else {
                String fileName = String.format((otherPath + File.separator + entityName + File.separator + "%s"), key);
                outputFile(new File(fileName), objectMap, value);
            }

        });
    }

    @NotNull
    @Override
    public AbstractTemplateEngine batchOutput() {
        try {
            ConfigBuilder config = this.getConfigBuilder();
            List<TableInfo> tableInfoList = config.getTableInfoList();
            tableInfoList.forEach(tableInfo -> {
                Map<String, Object> objectMap = this.getObjectMap(config, tableInfo);
                Optional.ofNullable(config.getInjectionConfig()).ifPresent(t -> {
                    t.beforeOutputFile(tableInfo, objectMap);
                    // objectMap.put("modelName", modelName);
                    objectMap.put("under_line_entity_name", StrUtil.toUnderlineCase(tableInfo.getName()));
                    objectMap.put("dtoName", StrUtil.upperFirst(tableInfo.getName()));
                    objectMap.put("id", "Long");
                    // 输出自定义文件
                    outputCustomFile(t.getCustomFile(), tableInfo, objectMap);
                });
                // Mp.java
                outputEntity(tableInfo, objectMap);
                // mapper and xml
                outputMapper(tableInfo, objectMap);
                // service
                outputService(tableInfo, objectMap);
                // MpController.java
                outputController(tableInfo, objectMap);
            });
        } catch (Exception e) {
            throw new RuntimeException("无法创建文件，请检查配置信息！", e);
        }
        return this;
    }
}
